<template>
	<view class="login">
		<view class="loginMethod">
			<view
				@click="clickLoginMethod('password')"
				:class="loginMethod=='password'?'hover':''">密码登陆</view>
			<view
				@click="clickLoginMethod('machineCode')"
				:class="loginMethod=='machineCode'?'hover':''">验证码登陆</view>
		</view>
		<view class="for">
			<view class="left">账号类型</view>
			<view class="right radio">
				<radio-group @change="radioAccountType">
					<label v-if="sendVerifyCodeStateEmail">
						<radio :checked="accountType == 'email'" value="email" /><text>邮箱</text>
					</label>
					<label v-if="sendVerifyCodeStatePhone">
						<radio :checked="accountType == 'phone'" value="phone" /><text>手机</text>
					</label>
				</radio-group>
			</view>
		</view>
		<view class="form">
			<template v-if="loginMethod == 'machineCode'">
				<view style="padding-top: 25rpx;line-height: 22rpx;text-align: center;color: red;font-size: 22rpx;">没有账号请直接登录，会自动生成账号</view>
				<view class="for">
					<text
						:class="accountType=='email'?'iconfont icon-fuzhi':'iconfont icon-shouji'"></text>
					<input
						v-model="account"
						:type="accountType=='email'?'text':'number'"
						:placeholder="accountType=='email'?'请输入邮箱地址':'请输入手机号'"
						placeholder-style="color:#C0C4CC;">
					<view
						v-if="sendVerifyCodeState == 'WeiFaSong'"
						@click="clickSendVerifyCode(accountType=='email'?'email':'sms')">发送验证码</view>
					<view
						v-if="sendVerifyCodeState == 'JieShouZhong'"
						class="Bj-ChangGuiSe"
						style="color: #FFF;background: #606266;">{{sendVerifyCodeCountdown}}秒后重发</view>
				</view>
				<view class="for">
					<text class="iconfont icon-keyboard-9"></text>
					<input
						v-model="password"
						placeholder="输入验证码"
						placeholder-style="color:#C0C4CC;">
				</view>
				<view
					@click="DianJi_DengLu('machineCode')"
					class="login-button"
					style="margin: 25rpx auto 0 auto;">验证码登陆</view>
			</template>
			<template v-if="loginMethod == 'password'">
				<view class="for">
					<text class="iconfont icon-user"></text>
					<input
						v-model="account"
						:type="accountType=='email'?'text':'number'"
						:placeholder="accountType=='email'?'输入邮箱地址':'请输入手机号'"
						placeholder-style="color:#C0C4CC;">
				</view>
				<view class="for">
					<text class="iconfont icon-lock"></text>
					<input
						v-model="password"
						type="text"
						placeholder="输入密码"
						:password="true"
						placeholder-style="color:#C0C4CC;">
				</view>
				<view
					@click="DianJi_DengLu('password')"
					class="login-button">账号登陆</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginMethod:"password",
				accountType:"email",
				account:"",
				password:"",
				sendVerifyCodeState:"WeiFaSong",
				sendVerifyCodeCountdown:60,
				sendVerifyCodeStateEmail:true,
				sendVerifyCodeStatePhone:true,
			}
		},
		onLoad(e) {
			if(e.type == 'machineCode'){
				this.loginMethod = "machineCode"
			}
			uni.request({
				url:this.$_Url() + "/api/v2/global/configs",
				method:'GET',
				header:this.$_FresnsHeader(),
				data:{
					"keys":"site_email_login,site_phone_login"
				},
				success: (res) => {
					this.sendVerifyCodeStateEmail = res.data.data.site_email_login;
					this.sendVerifyCodeStatePhone = res.data.data.site_phone_login;
				}
			})
		},
		methods: {
			// 点击，登陆，方式
			clickLoginMethod(e){
				this.loginMethod = e;
			},
			// 单选，账号类型
			radioAccountType(e){
				this.accountType = e.target.value
			},
			// 点击，发码
			clickSendVerifyCode(e){
				uni.showLoading({ title:"验证码发送中" });
				uni.request({
					url:this.$_Url() + "/api/v2/common/send-verify-code",
					method:'POST',
					header:this.$_FresnsHeader(),
					data:{
						"type":e,
						"useType":2,
						"templateId":7,
						"account":this.account,
						"countryCode":e=='email'?'':86,
					},
					success: (res) => {
						uni.hideLoading();
						if(res.data.code == 0){
							this.sendVerifyCodeState = 'JieShouZhong';
							this.DingShiQi_ShiJian = setInterval(() => {
								this.sendVerifyCodeCountdown -= 1;
								if(this.sendVerifyCodeCountdown == 0){
									clearInterval(this.DingShiQi_ShiJian)
									this.sendVerifyCodeCountdown = 60;
									this.sendVerifyCodeState = 'WeiFaSong';
								}
							}, 1000);
						}else{
							uni.showToast({
								title:res.data.message,
								icon:"none",
								duration:1000
							});
						}
					}
				})
			},
			// 点击，登陆
			DianJi_DengLu(e){
				if(e=='machineCode'){
					uni.showLoading({ title: "登录中" });
					uni.request({
						url:this.$_Url() + "/api/v2/account/login",
						method:'POST',
						header:this.$_FresnsHeader(),
						data:{
							"type":this.accountType, // 账号类型：email,phone
							"account":this.account, // 邮箱地址 / 手机号码
							"countryCode":86, // 国际区号
							"verifyCode":this.password, // 验证码登陆
						},
						success: (res) => {
							uni.hideLoading();
							if(res.data.message == '成功'){
								uni.setStorageSync("account",res.data);
								this.$_To("login/userSelection")
							}else{
								uni.showToast({
									title:res.data.message,
									icon:"none",
									duration:1000
								});
							}
						}
					})
				}
				if(e=='password'){
					uni.showLoading({ title: "登录中" });
					uni.request({
						url:this.$_Url() + "/api/v2/account/login",
						method:'POST',
						header:this.$_FresnsHeader(),
						data:{
							"type":this.accountType, // 账号类型：email,phone
							"account":this.account, // 邮箱地址 / 手机号码
							"countryCode":86, // 国际区号
							"password":this.$_stringToBase64(this.password), // 密码
							"verifyCode":"", // 验证码登陆
						},
						success: (res) => {
							uni.hideLoading();
							if(res.data.message == '成功'){
								uni.setStorageSync("account",res.data);
								this.$_To("login/userSelection")
							}else{
								uni.showToast({
									title:res.data.message,
									icon:"none",
									duration:1000
								});
							}
						}
					})
				}
			},
		}
	}
</script>

<style lang="scss">
.login {
	.login-button{
		background: $LanSe;
		color: #FFF;
		width: 200rpx;
		height: 80rpx;
		margin: 25rpx auto auto auto;
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: 10rpx;
	}
	view{
		font-size: 28rpx;
	}
	.loginMethod{
		margin: 50rpx auto 50rpx auto;
		border: 1rpx solid $BianKuangSe;
		width: 600rpx;
		height: 80rpx;
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		overflow: hidden;
		view{
			width: 50%;
			height: 80rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: $ZhanWeiSe;
		}
		.hover{
			background: $BeiJingSe;
			color: $YuanSe;
		}
	}
	.for{
		display: flex;
		border: 1rpx solid $BianKuangSe;
		margin: 25rpx 25rpx 0 25rpx;
		height: 80rpx;
		.left{
			font-size: 22rpx;
			line-height: 22rpx;
			background: $BeiJingSe;
			display: flex;
			align-items: center;
			justify-content: center;
			flex-shrink: 0;
			padding: 0 25rpx;
			border-right: 1rpx solid $BianKuangSe;
		}
		.right{
			flex: 1;
			height: 80rpx;
		}
		.radio{
			height: 80rpx;
			display: flex;
			align-items: center;
			radio-group{
				display: flex;
				align-items: center;
				label{
					display: flex;
					align-items: center;
					margin: 0 0 0 15rpx;
					radio{
						transform: scale(0.6);
						margin: 0 -10rpx 0 0;
					}
				}
			}
		}
	}
	.form{
		.for{
			display: flex;
			align-items: center;
			padding: 5rpx 25rpx;
			border-bottom: 1rpx solid $BianKuangSe;
			text{
				color: $ZhanWeiSe;
				margin: 0 15rpx 0 0;
			}
			input{
				font-size: 28rpx;
				flex: 1;
			}
			view{
				margin: 0 0 0 15rpx;
				display: inline-flex;
				background: $HuangSe;
				color: #000;
				font-size: 22rpx;
				line-height: 22rpx;
				border-radius: 100rpx;
				padding:15rpx 20rpx;
			}
		}
	}
}
</style>